package com.yuntsg.ruiijn.paperana.test;

import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 从字符串中提取出关键词，字符串可以是中文，也可以是英文，也可以是中英文。
 * 贾新志 2024.09.01
 */

public class extractEnCn {

    public static void main(String[] args) {
        String from = "Background Previous observational studies have shown a bidirectional association between immune-mediated inflammatory disorders (IMID) and periodontal disease. However, evidence regarding the causal role of IMID and periodontal disease is still lacking. Therefore, we conducted a bidirectional two-sample Mendelian randomization (MR) study to uncover the potential genetic causal effects between IMID and periodontal disease. Methods Bidirectional two-sample MR analysis was employed. Data for ten IMIDs were sourced from genome-wide association studies (GWAS) conducted by the FinnGen Consortium (range from 1023 to 36321 cases) and UK Biobank (UKB) (range from 150 to 17574 cases). Furthermore, GWAS data for periodontal disease were obtained from the FinnGen Consortium (87497 cases), UKB (458 cases), and Gene Lifestyle Interactions in Dental Endpoints (GLIDE) consortium (17,353 periodontitis cases). Subsequently, the causal relationships were analyzed by random effects inverse variance weighting, weighted median, and MR-Egger. Sensitivity analyses were performed using the Cochrane Q test, funnel plot, and Mr-Egger intercept test to ensure robustness. Eventually, replication analysis and meta-analysis across different databases were carried out. Results Systemic lupus erythematosus (SLE) [IVW: OR = 1.079 (95% CI: 1.032-1.128) and P < 0.001], Sjogren syndrome [IVW: OR = 1.082 (95% CI: 1.012-1.157) and P = 0.022] and hypothyroidism [IVW: OR = 1.52 (95% CI: 1.13-2.04) and P = 0.005] may increase the risk of periodontal disease. In addition, periodontal disease may reduce the risk of SLE [IVW: OR = 0.8079 (95% CI: 0.6764-0.9650) and P = 0.019] and hyperthyroidism [IVW: OR = 5.59*10(-9) (95% CI: 1.43*10(-15)-2.18*10(-2)) and P = 0.014]. Meta-analysis indicated a causal correlation between SLE and an increased risk of periodontal disease: [OR = 1.08 (95% CI: 1.03-1.13), P = 0.0009]. No significant evidence suggests bilateral causal relationships between other IMIDs and periodontal disease. No significant estimation of heterogeneity or pleiotropy is detected. Conclusions Our study has confirmed a genetic causal relationship between IMIDs and periodontal disease, thereby unveiling novel potential mechanisms underlying IMIDs and periodontal disease. This discovery is promising in fostering interdisciplinary collaboration between clinicians and stomatologists to facilitate appropriate and precise screening, prevention, and early treatment of IMIDs and periodontal disease. 背景先前的观察性研究表明免疫介导的炎症性疾病(IMID)与牙周病之间存在双向关联。然而，关于IMID和牙周病的因果作用的证据仍然缺乏。因此，我们进行了一项双向双样本孟德尔随机化(MR)研究，以揭示IMID与牙周病之间潜在的遗传因果关系。方法采用双向双样本磁共振分析。10个IMIDs的数据来自FinnGen Consortium(1023 - 36321例)和UK Biobank (UKB)(150 - 17574例)进行的全基因组关联研究(GWAS)。此外，牙周病的GWAS数据来自FinnGen联盟(87497例)、UKB(458例)和牙齿终点基因生活方式相互作用(GLIDE)联盟(17,353例牙周炎病例)。随后，采用随机效应反方差加权法、加权中位数法和MR-Egger法分析因果关系。采用Cochrane Q检验、漏斗图和Mr-Egger截距检验进行敏感性分析，以确保稳健性。最后，进行了跨不同数据库的复制分析和元分析。结果系统性红斑狼疮(SLE) [IVW: OR = 1.079 (95% CI: 1.032-1.128)， P < 0.001]、干燥综合征[IVW: OR = 1.082 (95% CI: 1.012-1.157)， P = 0.022]和甲状腺功能减退[IVW: OR = 1.52 (95% CI: 1.13-2.04)， P = 0.005]可增加牙周病的发生风险。此外，牙周病可降低SLE [IVW: OR = 0.8079 (95% CI: 0.6764-0.9650)， P = 0.019]和甲亢[IVW: OR = 5.59*10(-9) (95% CI: 1.43*10(-15)-2.18*10(-2))， P = 0.014]的风险。荟萃分析显示SLE与牙周病风险增加之间存在因果关系:[OR = 1.08 (95% CI: 1.03-1.13)， P = 0.0009]。没有显著证据表明其他IMIDs与牙周病之间存在双边因果关系。未发现异质性或多效性的显著估计。结论本研究证实了IMIDs与牙周病之间的遗传因果关系，从而揭示了IMIDs与牙周病的新的潜在机制。这一发现有望促进临床医生和口腔医生之间的跨学科合作，以促进适当和精确的筛查、预防和早期治疗IMIDs和牙周病。";
//        String from = "由于其早期症状不够明显，我国胃肠高发，患者常在中肿瘤普過";


        String[] split = from.replace("。", ". ").split("\\. ");
        for (String sentence : split) {
            List<String> list = new ArrayList<>();
            sentence = (isContainChinese(sentence)) ? extractCnEn(sentence, list) : sentence;
            List<Term> terms = StandardTokenizer.segment(sentence);
            for (Term term : terms) {
                String s = term.word.trim();
                s = s.startsWith(",") ? s.substring(1).trim() : s.trim();
                s = s.endsWith(",") ? s.substring(0, s.length() - 1).trim() : s.trim();
                if (s.length() > 1) {
                    list.add(s);
                }
            }
            for (String s : list) {
                System.out.println("--" + s);
            }
        }

    }

    // 把英文字符串和中文字符给予区分，英文的保存进enList，中文的给予返回。
    public static String extractCnEn(String from, List<String> enList) {
        Pattern pattern = Pattern.compile("[a-zA-Z() -:<>=%*]+|[\u4e00-\u9fa5]+");
        Matcher matcher = pattern.matcher(from.replace("，", ",").replace("。", ". ").replace("（", "(").replace("）", ")").replace("：", ": "));
        StringBuilder cn = new StringBuilder();
        while (matcher.find()) {
            String match = matcher.group().trim();
            if (match.matches("[a-zA-Z() -:<>=%*]+") && match.length() > 2) {
                String[] split = match.replace("(", "|").replace(")", "|").split("\\|");
                for (String s : split) {
                    s = s.startsWith(",") ? s.substring(1).trim() : s.trim();
                    s = s.endsWith(",") ? s.substring(0, s.length() - 1).trim() : s.trim();
                    if (s.length() > 2) {
                        enList.add(s);
                    }
                }
            } else if (match.length() > 1) {
                cn.append(match).append(" ");
            }
        }
        return cn.toString().trim();
    }

    // 判断是否包含中文汉字
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();
    }

    /*
              <dependency>
                  <groupId>com.hankcs</groupId>
                  <artifactId>hanlp</artifactId>
                  <version>portable-1.8.4</version>
              </dependency>
     */


}
